package com.example.librarymanagement.controller;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.librarymanagement.dto.type.TypeAddDto;
import com.example.librarymanagement.dto.type.TypeListDto;
import com.example.librarymanagement.dto.type.TypeUpdDto;
import com.example.librarymanagement.entity.Type;
import com.example.librarymanagement.result.ResultCode;
import com.example.librarymanagement.service.impl.TypeServiceImpl;
import com.example.librarymanagement.util.CheckEntityUtil;
import com.example.librarymanagement.vo.TypeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author lzx
 */
@Api(tags = "类型")
@RestController
@RequestMapping("/type")
public class TypeController extends BaseController {

    @GetMapping
    @ApiOperation(value = "查询图书类型信息")
    @PreAuthorize("hasAnyAuthority('type:manage:list','periodical:list:view','book:list:view')")
    public IPage<TypeVo> getBookTypeList(@Validated TypeListDto typeListDto) {
        return typeService.getBookType(typeListDto);
    }

    @GetMapping("/info/{id}")
    @ApiOperation(value = "查询图书类型信息")
    @PreAuthorize("hasAnyAuthority('type:manage:list','periodical:list:view','book:list:view')")
    public Type getBookTypeInfo(@PathVariable Integer id) {
        return typeService.getById(id);
    }

    @GetMapping("/list")
    @ApiOperation(value = "查询图书类型信息")
    @PreAuthorize("hasAnyAuthority('type:manage:list','periodical:list:view','book:list:view')")
    public List<Type> getBookTypeList() {
        return typeService.list();
    }

    @PutMapping
    @ApiOperation(value = "修改图书类型")
    @PreAuthorize("hasAuthority('type:manage:update')")
    public void updateType(@Validated @RequestBody TypeUpdDto typeUpdDto) {

        Type type = CheckEntityUtil.isNullById(typeUpdDto.getId(), typeService, ResultCode.BOOK_TYPE_NOT_EXIST);

        type.setCode(typeUpdDto.getCode());
        type.setTypeName(typeUpdDto.getTypeName());
        typeService.updateById(type);
    }

    @PostMapping
    @ApiOperation(value = "新增图书类型")
    @PreAuthorize("hasAuthority('type:manage:save')")
    public void addType(@Validated @RequestBody TypeAddDto typeAddDto) {

        CheckEntityUtil.isNotNullById(typeAddDto.getCode(), typeService, ResultCode.BOOK_TYPE_EXIST);

        Type type = new Type();
        typeAddDto.convertToEntity(type);

        typeService.save(type);
    }


    @DeleteMapping
    @ApiOperation(value = "删除图书类型")
    @PreAuthorize("hasAuthority('type:manage:delete')")
    @Transactional(rollbackFor = {Exception.class})
    public void deleteType(String ids) {
        List<Integer> typeIds = Arrays.asList(Convert.toIntArray(ids));
        typeService.removeByIds(typeIds);
    }
}
